<template>
  <div class="search-box">
    <div class="row">
      <div class="left">
        <el-icon>
          <Search />
        </el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="right">
        <el-button @click="
  queryParams.keyword = '';
queryUserList();
        ">重置</el-button>
        <el-button type="primary" @click="queryUserList">查询搜索</el-button>
      </div>
    </div>
    <div class="row">
      <span>账号/姓名:</span>
      <el-input placeholder="请输入姓名、账号搜索" v-model="queryParams.keyword"></el-input>
    </div>
  </div>
  <div class="add-area">
    <div class="add-icon">
      <el-icon>
        <Tickets />
      </el-icon>数据列表
    </div>
    <el-button type="primary" @click="showDialog = true" v-permission="'addUser'">添加</el-button>
  </div>
  <el-table :data="userList" border table-layout="auto" cell-class-name="align-c">
    <el-table-column label="编号" prop="id" />
    <el-table-column label="账号" prop="username" />
    <el-table-column label="姓名" prop="nickName" />
    <el-table-column label="邮箱" prop="email" />
    <el-table-column label="添加时间" prop="createTime">
      <template #default="{ row }">
        <span>{{ formateDate(row.createTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="最后登录" prop="loginTime">
      <template #default="{ row }">
        <span>{{ formateDate(row.loginTime) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="是否启用" prop="loginTime">
      <template #default="{ row }">
        <el-Switch v-model="row.status" :active-value="1" :inactive-value="0" @change="switchStatus(row)" />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button text type="primary" @click="allocRoleHandel(row.id)" v-permission="'allocRole'">分配角色
        </el-button>
        <el-button text type="primary" @click="editUser(row.id)" v-permission="'editUser'">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination v-model:current-page="queryParams.pageNum" v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]" background layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="queryUserList" @current-change="queryUserList" />
    </div>
  </ElConfigProvider>
  <UserEidt :show="showDialog" @hide="closeDialog" :id="editId" />
  <AllocRole :show="showAllocRole" :id="allocId" @hide="hideAlloc" />
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { userListApi, switchUserStatusApi } from "./api";
import { Search, Tickets } from "@element-plus/icons-vue";
// 国际化中文
import { ElConfigProvider, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
// 封装好的封装时间
import { formateDate } from "@/utils";
// 把添加用户的框封装一个组件
import UserEidt from "./editUser.vue";
import AllocRole from "./allocRole.vue";
// 定义一个响应式id值
const allocId = ref(0);
// 定义一个响应式控制分组的组件
const showAllocRole = ref(false);

const allocRoleHandel = (id: number) => {
  showAllocRole.value = true;
  allocId.value = id;
};
const hideAlloc = (isReload?: boolean) => {
  showAllocRole.value = false;
  allocId.value = 0;
};
//
const editId = ref(0);
//
const editUser = (id: number) => {
  editId.value = id;
  // 需要把id传入到userEdit组件
  // 把userEdit 显示出来
  showDialog.value = true;
};
// 控制添加用户框的显示与隐藏
const showDialog = ref(false);
// 关闭添加用户框的再次请求数据
const closeDialog = (isReload?: boolean) => {
  showDialog.value = false;
  editId.value = 0;
  if (isReload) queryUserList();
};
// 是否启用按钮
const switchStatus = (row: List) => {
  switchUserStatusApi(row.id!, row.status)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        // row.status = row.status === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      // 万一修改失败
      row.status = row.status === 1 ? 0 : 1;
    });
};
// 列表数据
const userList = ref<List[]>([]);
// 分页器的总共条数
const total = ref(0);
const queryParams = reactive<IUserListParams>({
  keyword: "",
  pageNum: 1,
  pageSize: 10,
});

// 查询的api的事件
const queryUserList = () => {
  userListApi(queryParams).then((res) => {
    if (res.code === 200) {
      userList.value = res.data.list;
      total.value = res.data.total;
    }
  });
};
// 一来到这个页面马上请求
queryUserList();
</script>
<style lang="less" scoped>
:deep(.align-c) {
  .cell {
    text-align: center !important;
  }
}

:deep(.cell) {
  text-align: center !important;
}

.search-box {
  box-shadow: 0 0 4px #ccc;
  border-radius: 10px;
  padding: 20px;
  margin: 20px 0;

  .row {
    padding: 20px 0;
    display: flex;

    &:first-child {
      justify-content: space-between;
    }

    &:last-child {
      width: 400px;

      span {
        width: 100px;
      }
    }
  }
}

.page {
  padding: 20px;
  display: flex;
  justify-content: flex-end;
}

.add-area {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  align-items: center;
  box-shadow: 0 0 4px #ccc;
  border-radius: 10px;
  padding: 20px;
  margin: 20px 0;

  .add-icon {
    display: flex;
    align-items: center;

    :deep(.el-icon) {
      margin-right: 10px;
    }
  }
}
</style>
